package com.aijk.web.admin.controller.ehm.crowd;

import com.aijk.commons.exception.ErrorCodeException;
import com.aijk.commons.lang.utils.StringUtil;
import com.aijk.commons.mijo.utils.PojoUtil;
import com.aijk.commons.utils.Utils;
import com.aijk.ehm.api.EhmBannerApi;
import com.aijk.ehm.api.EhmPeopleCrowdApi;
import com.aijk.ehm.api.EhmPeopleCrowdMutexApi;
import com.aijk.ehm.model.EhmPeopleCrowd;
import com.aijk.ehm.model.EhmPeopleCrowdMutex;
import com.aijk.ehm.model.qo.EhmPeopleCrowdMutexQo;
import com.aijk.ehm.model.qo.EhmPeopleCrowdQo;
import com.aijk.web.admin.RetMsg;
import com.aijk.web.admin.controller.BaseController;
import com.aijk.web.admin.model.CrowdExt;
import com.aijk.web.admin.utils.DataTableUtil;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 人群
 */
@Controller
@RequestMapping(value = "/admin/crowd")
public class CrowdController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(CrowdController.class);

    @Autowired
    private EhmBannerApi mhAdvertisementApi;

    @Autowired
    private EhmPeopleCrowdApi ehmPeopleCrowdApi;

    @Autowired
    private EhmPeopleCrowdMutexApi ehmPeopleCrowdMutexApi;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return "ehm/crowd/list";
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody public RetMsg list(@RequestParam HashMap skey,@RequestParam(value = "draw", defaultValue = "1") Integer draw) {
        Long iTotalRecords;
        DataTableUtil.FormatOptions(skey);
        if (Utils.isNotEmpty(skey.get("orderbyclause"))) {
            skey.put("orderbyclause", StringUtil.toUnderlineCase(Utils.toString(skey.get("orderbyclause"))));
        }
        EhmPeopleCrowdQo qo = DataTableUtil.fillQo(skey, EhmPeopleCrowdQo.class);
        draw += 1;
        PageInfo<EhmPeopleCrowd> mhItemPageInfo = ehmPeopleCrowdApi.page(qo);

        iTotalRecords =mhItemPageInfo.getTotal();
        RetMsg regMsg = new RetMsg();

        regMsg.put("data", mhItemPageInfo.getList());
        regMsg.put("draw", draw);
        regMsg.put("recordsTotal", iTotalRecords);
        regMsg.put("recordsFiltered", iTotalRecords);

        return regMsg;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Model model, Long id) {
        EhmPeopleCrowd bean = new EhmPeopleCrowd();
        if (id != null){
            	bean = ehmPeopleCrowdApi.findOne(id);
            model.addAttribute("method", "update");
        }else {
            model.addAttribute("method", "add");
        }
        model.addAttribute("crowd", bean);
        return "ehm/crowd/edit";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg save(EhmPeopleCrowd bean) {
            	Long id = bean.getId();
            	if (id == null) {// 添加
                    bean.setCreated(new Date());
                    ehmPeopleCrowdApi.save(bean);
            	} else {
                    //更新
                    ehmPeopleCrowdApi.update(bean);
                }
            return RetMsg.success();
        }


    @RequestMapping(value = "/additem", method = RequestMethod.GET)
    public String additem_view(@RequestParam(value = "id") Long id,Model model) {
        EhmPeopleCrowdQo qo = new EhmPeopleCrowdQo();
        if (id > 0){//排除自身
            qo.setNid(id);
        }
        EhmPeopleCrowdMutexQo qo1 = new EhmPeopleCrowdMutexQo();
        qo1.setPid(id);
        List<Long> ids = new ArrayList<>();
        List<EhmPeopleCrowdMutex> mutexes = ehmPeopleCrowdMutexApi.list(qo1);
        if (mutexes.size()>0){
            for (EhmPeopleCrowdMutex mutex:mutexes){
                ids.add(mutex.getHpid());
            }
            qo.setNids(ids);
        }
        List<EhmPeopleCrowd> ehmPeopleCrowds = ehmPeopleCrowdApi.list(qo);
        model.addAttribute("mid",id);
        model.addAttribute("stars",ehmPeopleCrowds);
        return "ehm/crowd/additem";
    }

    @RequestMapping(value = "/additem", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg additem(@Valid EhmPeopleCrowdMutex mhMemberItem) {
        RetMsg msg = new RetMsg();
        try {
            EhmPeopleCrowd mhItem = ehmPeopleCrowdApi.findOne(mhMemberItem.getHpid());
            Long id = ehmPeopleCrowdMutexApi.save(mhMemberItem);
            EhmPeopleCrowdMutex mutex = new EhmPeopleCrowdMutex();
            mutex.setHpid(mhMemberItem.getPid());
            mutex.setPid(mhMemberItem.getHpid());
            ehmPeopleCrowdMutexApi.save(mutex);
            msg.put("id",id);
            msg.put("mhMemberItem" , mhMemberItem);
            msg.put("itemtitle",mhItem.getTitle());
            msg.put("sn",mhItem.getSn());
            msg.put("code",0);
        }catch (ErrorCodeException e){
            e.printStackTrace();
            msg.put("code",-1);
        }
        return msg;
    }

    @RequestMapping(value = "/voitem", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg getitems(@RequestParam(value = "id") Long id ) {
        RetMsg msg = new RetMsg();
        EhmPeopleCrowdMutexQo ehmPeopleCrowdMutexQo = new EhmPeopleCrowdMutexQo();
        ehmPeopleCrowdMutexQo.setPid(id);
        List<EhmPeopleCrowdMutex> items = ehmPeopleCrowdMutexApi.list(ehmPeopleCrowdMutexQo);
        List<CrowdExt> list = new ArrayList<>();

        if (items!= null && items.size() > 0){
            list = PojoUtil.convert(items, CrowdExt.class);
            for (CrowdExt crowdExt : list){
                EhmPeopleCrowd crowd = ehmPeopleCrowdApi.findOne(crowdExt.getHpid());
                crowdExt.setHcrowd(crowd.getTitle());
                crowdExt.setSn(crowd.getSn());
            }
            msg.put("code" , 0);
            msg.put("items",list);
        }else {
            msg.put("code" , 0);
            msg.put("items",new ArrayList<>());
        }

        return msg;
    }



    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg deteleById(@RequestParam(value = "id") Long id) {
        try {
            ehmPeopleCrowdApi.deleteById(id);
        }catch (ErrorCodeException e){
            	e.printStackTrace();
            	logger.error(e.getMessage());
            	return RetMsg.failed();
        }
        return RetMsg.success();
    }

    @RequestMapping(value = "/delmx", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg del(@RequestParam(value = "id") Long id) {
        try {
            EhmPeopleCrowdMutex mutex = ehmPeopleCrowdMutexApi.findOne(id);
            Long pid = mutex.getPid();
            Long hpid = mutex.getHpid();
            EhmPeopleCrowdMutexQo qo = new EhmPeopleCrowdMutexQo();
            qo.setPid(hpid);
            qo.setHpid(String.valueOf(pid));
            List<EhmPeopleCrowdMutex> mutexes = ehmPeopleCrowdMutexApi.list(qo);
            if (mutexes.size()>0){
                ehmPeopleCrowdMutexApi.deleteById(mutexes.get(0).getId());
            }

            ehmPeopleCrowdMutexApi.deleteById(id);
        }catch (ErrorCodeException e){
            e.printStackTrace();
            return RetMsg.failed();
        }
        return RetMsg.success();
    }




}